#include <stdio.h>
#include <stdlib.h>
#include "./../inc/file_IO.h"
#include "./../inc/data.h"
#include "./../inc/flight_node.h"
#include "./../inc/general.h"
#include "./../inc/my_flight_node.h"
#include "./../inc/user_node.h"

void save_file_flight()
{
    FILE *fp;
    fp = fopen("./res/flight.bin", "wb");
    if (fp == NULL)
    {
        perror("open file error");
        return;
    }

    // 写入文件标识符
    const char magic[] = "FLIGHT_BIN";
    fwrite(magic, sizeof(char), strlen(magic), fp);

    flight_Node_p p = flight_headNode->next;

    while (p != flight_headNode && p->data != NULL)
    {
        // 直接写入整个数据结构
        size_t written = fwrite(p->data, sizeof(flight_t), 1, fp);
        if (written != 1)
        {
            perror("写入数据失败");
            break;
        }
        p = p->next;
    }

    fclose(fp);
}

flight_Node_p load_file_flight()
{
    FILE *fp = fopen("./res/flight.bin", "rb"); // 二进制读模式
    if (fp == NULL)
    {
        perror("open file error");
        return NULL;
    }

    // 检查文件标识符
    char magic[11];
    if (fread(magic, sizeof(char), 10, fp) != 10 ||
        strncmp(magic, "FLIGHT_BIN", 10) != 0)
    {
        printf("错误：不是有效的二进制文件格式\n");
        fclose(fp);
        return NULL;
    }
    magic[10] = '\0';

    flight_Node_p headNode = flight_create();
    if (!headNode)
    {
        fclose(fp);
        return NULL;
    }

    // 初始化空链表
    headNode->next = headNode;
    headNode->prev = headNode;
    flight_Node_p tail = headNode;

    while (1)
    {
        flight_Node_p new_node = (flight_Node_p)malloc(sizeof(flight_Node_t));
        flight_p data = (flight_p)malloc(sizeof(flight_t));
        if (new_node == NULL || data == NULL)
        {
            perror("malloc error");
            break;
        }

        // 读取整个数据结构
        size_t read = fread(data, sizeof(flight_t), 1, fp);
        if (read != 1)
        {
            // 可能到达文件末尾或读取错误
            free(data);
            free(new_node);
            break;
        }

        // 添加到链表
        new_node->data = data;
        new_node->next = headNode;
        new_node->prev = tail;

        tail->next = new_node;
        headNode->prev = new_node;
        tail = new_node;
    }

    fclose(fp);
    return headNode;
}

void save_file_my_flight()
{
    FILE *fp;
    fp = fopen("./res/my_flight.bin", "wb");
    if (fp == NULL)
    {
        perror("open file error");
        return;
    }

    // 写入文件标识符
    const char magic[] = "MY_FLIGHT_BIN";
    fwrite(magic, sizeof(char), strlen(magic), fp);

    my_flight_Node_p p = myflight_headNode->next;

    while (p != myflight_headNode && p->data != NULL)
    {
        // 直接写入整个数据结构
        size_t written = fwrite(p->data, sizeof(my_flight_t), 1, fp);
        if (written != 1)
        {
            perror("写入数据失败");
            break;
        }
        p = p->next;
    }

    fclose(fp);
}

my_flight_Node_p load_file_my_flight()
{
    FILE *fp = fopen("./res/my_flight.bin", "rb"); // 二进制读模式
    if (fp == NULL)
    {
        perror("open file error");
        return NULL;
    }

    // 检查文件标识符
    char magic[14];
    if (fread(magic, sizeof(char), 13, fp) != 13 ||
        strncmp(magic, "MY_FLIGHT_BIN", 13) != 0)
    {
        printf("错误：不是有效的二进制文件格式\n");
        fclose(fp);
        return NULL;
    }
    magic[13] = '\0';

    my_flight_Node_p headNode = my_flight_create();
    if (!headNode)
    {
        fclose(fp);
        return NULL;
    }

    // 初始化空链表
    headNode->next = headNode;
    headNode->prev = headNode;
    my_flight_Node_p tail = headNode;

    while (1)
    {
        my_flight_Node_p new_node = (my_flight_Node_p)malloc(sizeof(my_flight_Node_t));
        my_flight_p data = (my_flight_p)malloc(sizeof(my_flight_t));
        if (new_node == NULL || data == NULL)
        {
            perror("malloc error");
            break;
        }

        // 读取整个数据结构
        size_t read = fread(data, sizeof(my_flight_t), 1, fp);
        if (read != 1)
        {
            // 可能到达文件末尾或读取错误
            free(data);
            free(new_node);
            break;
        }

        // 添加到链表
        new_node->data = data;
        new_node->next = headNode;
        new_node->prev = tail;

        tail->next = new_node;
        headNode->prev = new_node;
        tail = new_node;
    }

    fclose(fp);
    return headNode;
}

void save_file_user()
{
    FILE *fp;
    fp = fopen("./res/user.bin", "wb");
    if (fp == NULL)
    {
        perror("open file error");
        return;
    }

    // 写入文件标识符
    const char magic[] = "USER_BIN";
    fwrite(magic, sizeof(char), strlen(magic), fp);

    user_Node_p p = user_headNode->next;

    while (p != user_headNode && p->data != NULL)
    {
        // 直接写入整个数据结构
        size_t written = fwrite(p->data, sizeof(user_t), 1, fp);
        if (written != 1)
        {
            perror("写入数据失败");
            break;
        }
        p = p->next;
    }

    fclose(fp);
}

user_Node_p load_file_user()
{
    FILE *fp = fopen("./res/user.bin", "rb"); // 二进制读模式
    if (fp == NULL)
    {
        perror("open file error");
        return NULL;
    }

    // 检查文件标识符
    char magic[10];
    if (fread(magic, sizeof(char), 8, fp) != 8 ||
        strncmp(magic, "USER_BIN", 8) != 0)
    {
        printf("错误：不是有效的二进制文件格式\n");
        fclose(fp);
        return NULL;
    }
    magic[9] = '\0';

    user_Node_p headNode = user_create();
    if (!headNode)
    {
        fclose(fp);
        return NULL;
    }

    // 初始化空链表
    headNode->next = headNode;
    headNode->prev = headNode;
    user_Node_p tail = headNode;

    while (1)
    {
        user_Node_p new_node = (user_Node_p)malloc(sizeof(user_Node_t));
        user_p data = (user_p)malloc(sizeof(user_t));
        if (new_node == NULL || data == NULL)
        {
            perror("malloc error");
            break;
        }

        // 读取整个数据结构
        size_t read = fread(data, sizeof(user_t), 1, fp);
        if (read != 1)
        {
            // 可能到达文件末尾或读取错误
            free(data);
            free(new_node);
            break;
        }

        // 添加到链表
        new_node->data = data;
        new_node->next = headNode;
        new_node->prev = tail;

        tail->next = new_node;
        headNode->prev = new_node;
        tail = new_node;
    }

    fclose(fp);
    return headNode;
}

// void save_file_flight()
// {
//     FILE *fp;
//     fp = fopen("./res/flight.bin", "w");
//     if (fp == NULL)
//     {
//         perror("open file error");
//         return;
//     }

//     flight_Node_p p = flight_headNode->next;
//     while (p != flight_headNode && p->data != NULL) // 添加data非空检查
//     {
//         fprintf(fp, "%s %s %s %s %s %s ",
//                 p->data->airline,
//                 p->data->flight_id,
//                 p->data->start,
//                 p->data->end,
//                 p->data->date,
//                 p->data->time);

//         // 分别输出数组
//         for (int i = 0; i < 5; i++)
//         {
//             fprintf(fp, "%d ", p->data->price[i]);
//         }
//         for (int i = 0; i < 5; i++)
//         {
//             fprintf(fp, "%d ", p->data->seat[i]);
//         }
//         for (int i = 0; i < 5; i++)
//         {
//             fprintf(fp, "%d ", p->data->remainder[i]);
//         }
//         fprintf(fp, "%d\n", p->data->status);

//         p = p->next;
//     }
//     fclose(fp);
// }

// void save_file_my_flight()
// {
//     FILE *fp;
//     fp = fopen("./res/my_flight.txt", "w");
//     if (fp == NULL)
//     {
//         perror("open file error");
//         return;
//     }
//     my_flight_Node_p p = myflight_headNode->next;
//     while (p != myflight_headNode && p->data != NULL) // 添加data非空检查
//     {
//         fprintf(fp, "%s %s %s %d %d %d\n", p->data->username, p->data->airline, p->data->flight_id, p->data->seat_type, p->data->price, p->data->status);
//         p = p->next;
//     }
//     fclose(fp);
// }

// flight_Node_p load_file_flight()
// {
//     FILE *fp = fopen("./res/flight.txt", "r");
//     if (fp == NULL)
//     {
//         perror("open file error");
//         return NULL;
//     }

//     flight_Node_p headNode = flight_create();
//     if (!headNode)
//         return NULL;

//     // 初始化空链表：头节点自循环
//     headNode->next = headNode;
//     headNode->prev = headNode;

//     flight_Node_p p = headNode;
//     while (1)
//     {
//         flight_Node_p new_node = (flight_Node_p)malloc(sizeof(flight_Node_t));
//         flight_p data = (flight_p)malloc(sizeof(flight_t));
//         if (new_node == NULL || data == NULL)
//         {
//             perror("malloc error");
//             break;
//         }

//         // 读取文件
//         int result = fscanf(fp, "%19s %19s %19s %19s %10s %5s",
//                             data->airline,
//                             data->flight_id,
//                             data->start,
//                             data->end,
//                             data->date,
//                             data->time);

//         if (result != 6)
//         {
//             free(data);
//             free(new_node);
//             break; // 文件结束或格式错误
//         }

//         // 读取价格数组
//         for (int i = 0; i < 5; i++)
//         {
//             fscanf(fp, "%d", &data->price[i]);
//         }

//         // 读取座位数组
//         for (int i = 0; i < 5; i++)
//         {
//             fscanf(fp, "%d", &data->seat[i]);
//         }

//         // 读取余票数组
//         for (int i = 0; i < 5; i++)
//         {
//             fscanf(fp, "%d", &data->remainder[i]);
//         }

//         // 读取状态
//         fscanf(fp, "%d", &data->status);

//         new_node->data = data;
//         new_node->next = NULL;
//         new_node->prev = p;
//         p->next = new_node;

//         p = p->next;
//     }

//     if (headNode->next != NULL)
//     {
//         p->next = headNode;
//         headNode->prev = p;
//     }

//     fclose(fp);
//     return headNode;
// }

// void save_file_user()
// {
//     FILE *fp;
//     fp = fopen("./res/user.txt", "w");
//     if (fp == NULL)
//     {
//         perror("open file error");
//         return;
//     }
//     user_Node_p p = user_headNode->next;
//     while (p != user_headNode && p->data != NULL)
//     {
//         fprintf(fp, "%s %s %s %s %d %d\n", p->data->username, p->data->password, p->data->phone, p->data->airline, p->data->user_type, p->data->money);
//         p = p->next;
//     }
//     fclose(fp);
// }

// user_Node_p load_file_user()
// {
//     FILE *fp = fopen("./res/user.txt", "r");
//     if (fp == NULL)
//     {
//         perror("open file error");
//         return NULL;
//     }

//     user_Node_p headNode = user_create();
//     if (!headNode)
//         return NULL;

//     // 初始化空链表：头节点自循环
//     headNode->next = headNode;
//     headNode->prev = headNode;

//     user_Node_p p = headNode;

//     while (1)
//     {
//         user_Node_p new_node = (user_Node_p)malloc(sizeof(user_Node_t));
//         user_p data = (user_p)malloc(sizeof(user_t));
//         if (new_node == NULL || data == NULL)
//         {
//             perror("malloc error");
//             break;
//         }

//         // 读取文件
//         int result = fscanf(fp, "%s %s %s %s %d %d", data->username, data->password, data->phone, data->airline, &data->user_type, &data->money);

//         if (result != 6)
//         {
//             free(data);
//             free(new_node);
//             break; // 文件结束或格式错误
//         }
//         new_node->data = data;
//         new_node->next = NULL;
//         new_node->prev = p;
//         p->next = new_node;

//         p = p->next;
//     }

//     if (headNode->next != NULL)
//     {
//         p->next = headNode;
//         headNode->prev = p;
//     }
//     fclose(fp);
//     return headNode;
// }

// my_flight_Node_p load_file_my_flight()
// {
//     FILE *fp = fopen("./res/my_flight.txt", "r");
//     if (fp == NULL)
//     {
//         perror("open file error");
//         return NULL;
//     }

//     my_flight_Node_p headNode = my_flight_create();
//     if (!headNode)
//         return NULL;

//     // 初始化空链表：头节点自循环
//     headNode->next = headNode;
//     headNode->prev = headNode;

//     my_flight_Node_p p = headNode;
//     while (1)
//     {
//         my_flight_Node_p new_node = (my_flight_Node_p)malloc(sizeof(my_flight_Node_t));
//         my_flight_p data = (my_flight_p)malloc(sizeof(my_flight_t));
//         if (new_node == NULL || data == NULL)
//         {
//             perror("malloc error");
//             break;
//         }

//         // 读取文件
//         int result = fscanf(fp, "%s %s %s %d %d %d", data->username, data->airline, data->flight_id, &data->seat_type, &data->price, &data->status);

//         if (result != 6)
//         {
//             free(data);
//             free(new_node);
//             break; // 文件结束或格式错误
//         }

//         new_node->data = data;
//         new_node->next = NULL;
//         new_node->prev = p;
//         p->next = new_node;

//         p = p->next;
//     }

//     if (headNode->next != NULL)
//     {
//         p->next = headNode;
//         headNode->prev = p;
//     }

//     fclose(fp);
//     return headNode;
// }